// pages/user/card.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipData:{},
    vip_id:-1,
    userInfo:{},
    show_open_vip:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({ extConfig: app.getExtConfigSync() })
    app.getUserInfo(function (userInfo) {
      if (!userInfo.store_member.user_tag){
         var show_open_vip = 1;
      }
      that.setData({ userInfo: userInfo, show_open_vip: show_open_vip}) ;
    })
    this.vipInfo();
    this.getInfo();
  },


  getInfo: function () {
    var that = this;
    app.api("api/user/info", {
      fields: "avatarUrl,user_id,last_update_time,nickname,phone"
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({ user_info: res.data });
    })
  },


  selectVip:function(e){
    var name = e.currentTarget.dataset.name;
    this.setData({
      vip_id:name
    }); 
    var vipInfo = this.data.vipData.list[this.data.vip_id];
    this.setData({
      vipInfo: vipInfo,
    });
    
  },
  openVip:function(){

    if (!this.data.user_info.phone) {
      wx.showModal({
        title: '提示',
        cancelText: '取消',
        confirmText: '去绑定手机',
        content: '为了保障您的会员权益提供更好的服务，需要绑定手机',
        success(res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            wx.navigateTo({
              url: '/pages/user/bindphone?page=bindphone&back=1',
            })
          } else if (res.cancel) {

          }
        }
      })


      return ;
    }

    if (this.data.vip_id<0){
      app.alert("请先选择会员类型");
      return false;
    }
    
    var vipInfo = this.data.vipData.list[this.data.vip_id];
    if (vipInfo.type =="free"){
      this.openFree();
    } else if (vipInfo.type == "charge"){
      this.openCharge();
    }
  },
  openCharge:function(){
    var vipInfo = this.data.vipData.list[this.data.vip_id];
    var chargeSet = vipInfo.openSetting.chargeSet;
    this.data.recharge_id = chargeSet;
    this.doRecharge();
  },
  suc:function(){
    app.alert("恭喜您,会员开通成功!");
    app.setPageData("user/index", { reload: 1 });
    wx.navigateBack();
  },

  openFree: function(){
    var that = this;
    app.api("api/user/openVip", {
      vip_id: this.data.vip_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.suc();
    })
  },
  vipInfo: function () {
    var that = this;
    app.api("mall/promotion/find", {
      name: 'VIP_Discounts'
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({
        vipData: res.data
      });
      console.log("vipData", that.data.vipData);

    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})